﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

namespace Strava1
{
    public partial class SegmentDetails : PhoneApplicationPage
    {
        int SegmentId;
        public SegmentDetails()
        {
            InitializeComponent();
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            SegmentId = int.Parse(NavigationContext.QueryString["id"]);
            Segment segment = App.ViewModel.Segments.First(s => s.Id == SegmentId);


            if (segment.Efforts == null)
            {
                segment.PopulateEfforts();
            }
            if (segment.MyEfforts == null)
            {
                segment.PopulateMyEfforts();
            }
            if (segment.MapLocations == null)
            {
                segment.PopulateMap();
            }

            DataContext = App.ViewModel;
            LayoutRoot.DataContext = segment;

            if (e.NavigationMode != System.Windows.Navigation.NavigationMode.Back)
            {

            }
        }

        private void MyEfforts_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count > 0)
            {
                Effort effort = (Effort)e.AddedItems[0];
                if (effort.Ride != null)
                {
                        NavigationService.Navigate(new Uri("/RideDetails.xaml?id=" + effort.Ride.Id, UriKind.Relative));
                }
            }
            ((ListBox)sender).SelectedIndex = -1;
        }

        private void Refresh_Click(object sender, EventArgs e)
        {
            App.ViewModel.RefreshSegmentEfforts(((Segment)LayoutRoot.DataContext).Id);
        }
    }
}